package com.yiku.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.yiku.common.DingConstant;
import com.yiku.service.dingding.DingdingService;
import com.yiku.utils.HttpRequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:ludada
 * @Date:2023/10/08 09:31
 */
@RestController
@Api(tags = "钉钉事件回调")
@RequestMapping("/dingc")
public class CallBackController {
    @Autowired
    DingdingService dingService;


    @ApiOperation("配置stream")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "企业内部应用 AppKey/第三方企业应用 SuiteKey"),
            @ApiImplicitParam(name = "clientSecret", value = "企业内部应用AppSecret/第三方企业应用 SuiteSecret"),
            @ApiImplicitParam(name = "url", value = "回调转发地址,默认请求方式post", defaultValue = "http://localhost:8052/dingc/callBack")
    })
    @GetMapping("/setCallBack")
    public void setCallBack(@RequestParam String clientId, @RequestParam String clientSecret, @RequestParam String url) {
        try {
            OpenDingTalkStreamClientBuilder
                    .custom()
                    .credential(new AuthClientCredential(clientId, clientSecret))
                    //注册事件监听
                    .registerAllEventListener(new GenericEventListener() {
                        public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                            try {
                                //事件唯一Id
                                String eventId = event.getEventId();
                                //事件类型
                                String eventType = event.getEventType();
                                //事件产生时间
                                Long bornTime = event.getEventBornTime();
                                //获取事件体
                                JSONObject bizData = event.getData();
                                //处理事件
                                HttpRequestUtil.sendPost(url, JSON.toJSONString(bizData));
                                //消费成功
                                return EventAckStatus.SUCCESS;
                            } catch (Exception e) {
                                //消费失败
                                return EventAckStatus.LATER;
                            }
                        }
                    })
                    .build().start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("接收事件回调")
    @PostMapping("/callBack")
    public void callBack(@RequestBody JSONObject json) {


            //U9C钉钉质检
            if (json.getString("processCode").equals(DingConstant.PROCESS_CODE)) {
            System.out.println(json);
                dingService.callBack(json);

            }


    }

}
